package dmt16.TelemetrySuit.UI
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.ISensorType;
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Tiles.TileBase;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class DetailView extends Sprite
	{
		public function DetailView()
		{
			backBtn=new BackBtn();
			addChild(backBtn);
			backBtn.x=40;
			backBtn.y=40;
			
			_title=new TextField();
			_title.embedFonts=true;
			_title.selectable=false;
			_title.defaultTextFormat=_textFormat;
			addChild(_title);
						
			backBtn.addEventListener(MouseEvent.CLICK,_toggleView);
		}
		
		private var _tile:TileBase;	
		private var backBtn:BackBtn;
		private var _title:TextField;
		
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigitalBold;
			_textFormat.size=70;
			_textFormat.align=TextFormatAlign.LEFT;
			_textFormat.color=Colours.FG;
		}
		
		private function _toggleView(event:MouseEvent):void{
			(parent as MainWindow).mainView=true;
			releaseControl();
		}
		
		public function giveControl(sensorTile:TileBase):void{
			releaseControl();
			_tile=sensorTile;
			addChild(_tile.detailContent);
			_tile.hasControl=true;
			_redraw();
		}
		
		public function releaseControl():void{
			if(_tile!=null){
				removeChild(_tile.detailContent);
				_tile.hasControl=false;
				_tile=null;
			}
		}
		
		private function _redraw():void{
			if(_tile!=null){
				_title.text=_tile.title;
				_title.x=100;
				_title.y=10;
				_title.width=_width*0.9-100;
				_title.height=60;
				_tile.detailContent.width=_width*0.9;
				_tile.detailContent.height=_height*0.9-60;
				_tile.detailContent.x=_width*0.05;
				_tile.detailContent.y=_height*0.05+60;
				_tile.drawDetailContent();
			}
		}
		
		private var _width:Number=100;
		public override function get width():Number{
			return _width;
		}
		public override function set width(value:Number):void{
			_width=value;
			_redraw();
		}
		
		private var _height:Number=250;
		public override function get height():Number{
			return _height;
		}
		public override function set height(value:Number):void{
			_height=value;
			_redraw();
		}
	}
}